/**
 *
 * by littlefean
 */

window.onload = function () {
    let dec = document.querySelector(`.dec`);

    let o2 = document.querySelector(`.out2`);
    let o8 = document.querySelector(`.out8`);
    let o16 = document.querySelector(`.out16`);

    let o = document.querySelector(`.out`);

    /**
     * 根据一个十进制数更改所有结果
     * @param decNum
     */
    function update(decNum) {
        dec.value = decNum.toString();
        o2.value = (+(decNum)).toString(2);
        o8.value = (+(decNum)).toString(8);
        o16.value = (+(decNum)).toString(16);
        updateAll();
    }

    /**
     * 十进制数转N进制数
     * @param n N进制数
     */
    function get(n) {
        let res = (+(dec.value)).toString(n);
        let title = div(`${n}进制结果`, "note");
        let result = div(res, "ans");
        let piece = div("", "piece");
        piece.appendChild(title);
        piece.appendChild(result);
        return piece;
    }

    function updateAll() {
        o.innerHTML = "";
        for (let i = 2; i <= 36; i++) {
            o.appendChild(get(i));
        }
    }

    /**
     * 十进制更改
     */
    dec.onblur = function () {
        o2.value = (+(dec.value)).toString(2);
        o8.value = (+(dec.value)).toString(8);
        o16.value = (+(dec.value)).toString(16);

        updateAll();
    }

    /**
     * 二进制更改
     */
    o2.onblur = function () {
        let decNum = parseInt(o2.value, 2);
        update(decNum);
    }
    o8.onblur = function () {
        let decNum = parseInt(o8.value, 8);
        update(decNum);
    }
    o16.onblur = function () {
        let decNum = parseInt(o16.value, 16);
        update(decNum);
    }
}
